﻿using System.Threading.Tasks;
using System.Windows.Forms;
using Common;
using DevExpress.Utils;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using FileHelpers;

namespace DataExporter
{
    class Helper
    {
        public static IDataLayer ConnectToDb(string server, string user, string pwd)
        {
            var connstring =
                OracleConnectionProvider.GetConnectionString(server, user, pwd);
            var dl = XpoDefault.GetDataLayer(connstring, AutoCreateOption.SchemaAlreadyExists);
            return dl;

        }

        public static void InitGrid(GridControl gridControl, GridView gridView)
        {
            int idx = gridView.Columns.Count-1;

            var colHphm = new GridColumn();
            colHphm.FieldName = "LprNumber";
            colHphm.Caption = "车牌号码";
            colHphm.Visible = true;
            colHphm.VisibleIndex = ++idx;

            var lookupeditViolationCode = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            gridControl.RepositoryItems.Add(lookupeditViolationCode);
            lookupeditViolationCode.AutoHeight = false;
            lookupeditViolationCode.Name = "repositoryItemLookupEditViolationCode";
            lookupeditViolationCode.DataSource = AppConst.ViolationCodes;
            lookupeditViolationCode.ValueMember = "Value";
            lookupeditViolationCode.DisplayMember = "Name";
            lookupeditViolationCode.Columns.Add(new LookUpColumnInfo("Name", "违法代码"));


            var colWzyy = new GridColumn();
            colWzyy.FieldName = "ViolationCode";
            colWzyy.Caption = "违法代码";
            colWzyy.Visible = true;
            colWzyy.VisibleIndex = ++idx;
            colWzyy.ColumnEdit = lookupeditViolationCode;

            var lookupeditRoadCode = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            gridControl.RepositoryItems.Add(lookupeditRoadCode);
            lookupeditRoadCode.AutoHeight = false;
            lookupeditRoadCode.Name = "repositoryItemComboBox1";
            lookupeditRoadCode.DataSource = AppConst.Roads;
            lookupeditRoadCode.ValueMember = "Value";
            lookupeditRoadCode.DisplayMember = "Name";
            lookupeditRoadCode.Columns.Add(new LookUpColumnInfo("Name", "道路代码"));


            var colRoadCode = new GridColumn();
            colRoadCode.FieldName = "RoadNo";
            colRoadCode.Caption = "道路代码";
            colRoadCode.Visible = true;
            colRoadCode.VisibleIndex = ++idx;
            colRoadCode.ColumnEdit = lookupeditRoadCode;

            var lookupEditCarType = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            gridControl.RepositoryItems.Add(lookupEditCarType);
            lookupEditCarType.AutoHeight = false;
            lookupEditCarType.Name = "repositoryItemComboBox1";
            lookupEditCarType.DataSource = AppConst.CarTypes;
            lookupEditCarType.ValueMember = "Value";
            lookupEditCarType.DisplayMember = "Name";
            lookupEditCarType.Columns.Add(new LookUpColumnInfo("Name", "车辆类型"));


            var colType = new GridColumn();
            colType.FieldName = "CarType";
            colType.Caption = "车辆类型";
            colType.VisibleIndex = ++idx;
            colType.ColumnEdit = lookupEditCarType;


            var colTime = new GridColumn();
            colTime.FieldName = "CaptureTime";
            colTime.Caption = "违法时间";
            colTime.DisplayFormat.FormatString = "MM.dd HH:mm";
            colTime.DisplayFormat.FormatType = FormatType.DateTime;
            colTime.Visible = true;
            colTime.VisibleIndex = ++idx;
            colTime.OptionsColumn.AllowEdit = false;


            gridView.Columns.AddRange(new[] { colHphm, colWzyy, colTime, colRoadCode, colType });

        }

        public static string GetSaveFileName()
        {
            var dlg = new SaveFileDialog();
            dlg.DefaultExt = AppConst.FileExtension;
            dlg.Filter = AppConst.FilterExtension;

            if (dlg.ShowDialog() != DialogResult.OK) return null;
            return dlg.FileName;

        }

        public static string GetOpenFileName()
        {
            var dlg = new OpenFileDialog();
            dlg.DefaultExt = AppConst.FileExtension;
            dlg.Filter = AppConst.FilterExtension;

            if (dlg.ShowDialog() != DialogResult.OK) return null;
            return dlg.FileName;

        }

        public static Task<Record[]> ReadFileAsync(string fileName)
        {
            return TaskEx.Run(() =>
            {
                var engine = new DelimitedFileEngine(typeof(Record));
                return engine.ReadFile(fileName) as Record[];
            });

        }

        public static Task WriteFileAsync(string fileName, Record[] records)
        {
            return TaskEx.Run(() =>
            {
                var engine = new DelimitedFileEngine(typeof(Record));
                engine.WriteFile(fileName, records);
            });

        }

    }
}
